define (require)->
    $ = require('jquery')
    move = require('../lib/move.min.js')

    scroll = require('./scroll.coffee') 

    require('./focus.scss')
    $focus = null

    eo={}
    eo.init = ->
        $focus = $("<div>", {class:"focus-shadow"})
        if !$('body > .focus-shadow').length
            $('body').append $focus
        else
            $focus = $('body > .focus-shadow')

    eo.move = (action)->
        $current = $('.focus')
        return if !$current.length
        level = $current.attr("level")
        nextL = $current.attr(action)

        changeLevel = false
        if nextL
            level = nextL
            changeLevel = true

        $all = $(".focus-l-#{level}")
        if chageLevel?
            #index = 
        else
            count = $all.length
            index = $all.index($current) || 0
                            
            if action=="left" || action=="up"
                index--
                index = count - 1 if index < 0
            else
                index++
                index = 0 if index >= count

        $next = $all.eq(index)

        return if !$focus || !$focus.length
        $focus.show()
        $current.removeClass('focus').blur()
        move($focus[0])
        .set("width", $current[0].offsetWidth+"px")
        .set("height", $current[0].offsetHeight+"px")
        .set("border-radius", $current.css("border-radius"))
        .set("z-index", $current.css("z-index")-1)
        .set("left", $current.offset().left+"px")
        .set("top", $current.offset().top+"px")
        .duration(0)
        .then ->
            $focus.show()
            move($focus[0])
            .ease('snap')
            .set("width", $next[0].offsetWidth+"px")
            .set("height", $next[0].offsetHeight+"px")
            .set("border-radius", $next.css("border-radius"))
            .set("z-index", $next.css("z-index")-1)
            .set("left", $next.offset().left+"px")
            .set("top", $next.offset().top+"px")
            .duration(200)
            .end ->
               $focus.hide()
               scroll.height($next, action)
        .end ->
           $next.addClass('focus').focus()

    eo